package thirdQuestion;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private final static String name = "蒋金鑫";
    private final static String password="951218";

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        ObjectInputStream ois =null;
        PrintWriter pw = null;
        try {
            serverSocket = new ServerSocket(8888);
            System.out.println("服务端已经启动。。。。等待客户端连接。。。");
            Socket socket =serverSocket.accept();
            System.out.println("客户端已经连接上了。。。"+socket.getInetAddress());

            ois = new ObjectInputStream(socket.getInputStream());
            pw = new PrintWriter(socket.getOutputStream(),true);

            User user= (User) ois.readObject();
            if (user.getName().equals(name)){
                if (user.getPassword().equals(password)){
                    System.out.println("客户端登陆成功..."+socket.getInetAddress());
                    pw.println("登陆成功！");
                }else {
                    System.out.println("他的密码错了");
                    pw.println("密码输错了哦!");
                }
            }else {
                System.out.println("他的用户名输错了");
                pw.println("用户名错了哦！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
